package com.xiaoming.e3mall.sso.service.impl;

import com.xiaoming.e3mall.common.jedis.JedisClient;
import com.xiaoming.e3mall.common.utils.E3Result;
import com.xiaoming.e3mall.common.utils.JsonUtils;
import com.xiaoming.e3mall.pojo.TbUser;
import com.xiaoming.e3mall.sso.service.TokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 根据token取用户信息
 */
@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private JedisClient jedisClient;

    @Value("${SESSION_EXPIRE}")
    private Integer SESSION_EXPIRE;
    @Override
    public E3Result getUserByToken(String token) {
        //根据浏览器发过来的token从redis中取用户信息
        String json = jedisClient.get("SESSION:" + token);
        //取不到信息,返回还没登录
        if(StringUtils.isBlank(json))
            return E3Result.build(201, "用户还没登录");

        //取到信息,更新token的过期时间
        jedisClient.expire("SESSION:"+token, SESSION_EXPIRE);

        //返回结果
        TbUser user = JsonUtils.jsonToPojo(json, TbUser.class);
        return E3Result.ok(user);
    }
}
